if (localStorage.getItem("id") && localStorage.getItem("token")) {
    $.ajax({
        url: "http://localhost:8888/users/info",
        method: "get",
        data: {
            id: localStorage.getItem("id")
        },
        headers: {
            authorization: localStorage.getItem("token")
        },
        success: function (res) {
            console.log(res);
            yonghu(res.info.nickname)
            tuichu()
        }
    })

} else {
    console.log("没有");
}
function yonghu(a) {
    $(".header-left").html(`
    <span>您好! ${a} </span>
    <a href="login.html">退出</a>

    `)
}
function tuichu() {
    $(".header-left>a").on("click", function () {
        if (confirm("是否确定退出")) {
            $.ajax({
                url: "http://localhost:8888/users/logout",
                method: "get",
                headers: {
                    authorization: localStorage.getItem("token")
                },
                data: {
                    id: localStorage.getItem("id")
                },
                success: function (res) {
                    console.log(res);
                    alert(res.message)
                    location.href = "home.html"
                    localStorage.removeItem("id")
                    localStorage.removeItem("token")
                }
            })
        }
        return false
    })
}
$(".geren>p:nth-of-type(4)").on("click", function () {
    if (localStorage.getItem("id") && localStorage.getItem("token")) {
        location.href = `account.html`
    } else {
        alert("请先登录")
    }
    return false
})


$("body>section>.box2 .box2-top").html(`
<ul>
    <li>
        <p>欧洲直购</p>
        <span>GO</span>
        <img src="imgs/list1.png" alt="">
    </li>
    <li>
        <p>日本直购</p>
        <span>GO&gt;</span>
        <img src="imgs/list2.png" alt="">
    </li>
    <li>
        <p>美国直购</p>
        <span>GO&gt;</span>
        <img src="imgs/list3.png" alt="">
    </li>
    <li>
        <p>韩国直购</p>
        <span>GO&gt;</span>
        <img src="imgs/list4.png" alt="">
    </li>
    <li>
        <p>香港直购</p>
        <span>GO&gt;</span>
        <img src="imgs/list5.png" alt="">
    </li>
    <li>
        <p>澳新直购</p>
        <span>GO&gt;</span>
        <img src="imgs/list6.png" alt="">
    </li>
</ul>
`)
// 
// 热销爆款

console.log("电脑办公");
console.log(`${localStorage.getItem("category")}`);
console.log("电脑办公"===`${localStorage.getItem("category")}`);
$.ajax({
    url: "http://localhost:8888/goods/list",
    data: {
        // filter:"hot",
        pagesize: "1000",
        search: localStorage.getItem("value"),
        category:localStorage.getItem("category"),
        // category:"电脑办公",
    },
    success: function (res) {

        if (res.list.length > 21) {
            if(localStorage.getItem("value")){
                $("body>section>.box2 .hot1>p")[0].innerText = `-${localStorage.getItem("value")}-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `${localStorage.getItem("value")}`
            }else if(localStorage.getItem("category")){
                $("body>section>.box2 .hot1>p")[0].innerText = `-${localStorage.getItem("category")}-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `${localStorage.getItem("category")}`
            }else{
                $("body>section>.box2 .hot1>p")[0].innerText = `-热销爆款-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `热销爆款`
            }
            
            const num = res.list.length - 21
            const index = Math.floor(Math.random() * num)
            var str = ``
            for (let i = index; i < index + 20; i++) {
                str += `
            <div>
                <img src="${res.list[i].img_big_logo}" alt="" index="${res.list[i].goods_id}">
                <p>${res.list[i].title}</p>
                <span>
                    <h3>现价￥${res.list[i].current_price}</h3>
                    <del>原价￥${res.list[i].price}</del>
                    <button index="${res.list[i].goods_id}">立即购买</button>
                </span>
            </div>
            `
            }
            $("body>section>.box2 .hot1>section")[0].innerHTML = str
            $("body>section>.box2 .hot1>section button").on("click", function () {
                localStorage.setItem("gods_id", $(this).attr("index"))
                location.href = "data.html"
            })
            $("body>section>.box2 .hot1>section img").on("click", function () {
                localStorage.setItem("gods_id", $(this).attr("index"))
                location.href = "data.html"
            })
        }else if(res.list.length==0){
            $("body>section>.box2>.box3 a:nth-of-type(3)")[0].innerText = `猜你喜欢`
            var str = `
            <strong>
                未找到关于${localStorage.getItem("value")}的商品
            </strong>
            `
            $("body>section>.box2 .hot1>section")[0].innerHTML = str
            $("body>section>.box2 .hot1>section strong").css({
                width:"1000px",
                height:"200px",
                border:"1px solid rgb(221,221,221)",
                textAlign:"center",
                lineHeight:"200px",
                height:"200px",
                fontSize:"40px",
                fontStyle:"normal",
                fontFamily:"楷体",
            })
            $("body>section>.box2 .hot2>p")[0].innerText = `-猜你喜欢-`
        }else if(res.list.length<5){
            if(localStorage.getItem("value")){
                $("body>section>.box2 .hot1>p")[0].innerText = `-${localStorage.getItem("value")}-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `${localStorage.getItem("value")}`
            }else if(localStorage.getItem("category")){
                $("body>section>.box2 .hot1>p")[0].innerText = `-${localStorage.getItem("category")}-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `${localStorage.getItem("category")}`
            }else{
                $("body>section>.box2 .hot1>p")[0].innerText = `-热销爆款-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `热销爆款`
            }
            var str = ``
            for (let i = 0; i < res.list.length; i++) {
                str += `
            <div>
                <img src="${res.list[i].img_big_logo}" alt="" index="${res.list[i].goods_id}">
                <p>${res.list[i].title}</p>
                <span>
                    <h3>现价￥${res.list[i].current_price}</h3>
                    <del>原价￥${res.list[i].price}</del>
                    <button index="${res.list[i].goods_id}">立即购买</button>
                </span>
            </div>
            `
            }
            $("body>section>.box2 .hot1>section")[0].innerHTML = str
            $("body>section>.box2 .hot1>section button").on("click", function () {
                localStorage.setItem("gods_id", $(this).attr("index"))
                location.href = "data.html"
            })
            $("body>section>.box2 .hot1>section img").on("click", function () {
                localStorage.setItem("gods_id", $(this).attr("index"))
                location.href = "data.html"
            })
            $("body>section>.box2 .hot1>section ").css({
                height:"400px",
            })
        }else{
            if(localStorage.getItem("value")){
                $("body>section>.box2 .hot1>p")[0].innerText = `-${localStorage.getItem("value")}-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `${localStorage.getItem("value")}`
            }else if(localStorage.getItem("category")){
                $("body>section>.box2 .hot1>p")[0].innerText = `-${localStorage.getItem("category")}-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `${localStorage.getItem("category")}`
            }else{
                $("body>section>.box2 .hot1>p")[0].innerText = `-热销爆款-`
                $("body>section>.box2>.box3 a:nth-of-type(2)")[0].innerText = `热销爆款`
            }
            var str = ``
            for (let i = 0; i < res.list.length; i++) {
                str += `
            <div>
                <img src="${res.list[i].img_big_logo}" alt="" index="${res.list[i].goods_id}">
                <p>${res.list[i].title}</p>
                <span>
                    <h3>现价￥${res.list[i].current_price}</h3>
                    <del>原价￥${res.list[i].price}</del>
                    <button index="${res.list[i].goods_id}">立即购买</button>
                </span>
            </div>
            `
            }
            $("body>section>.box2 .hot1>section")[0].innerHTML = str
            $("body>section>.box2 .hot1>section button").on("click", function () {
                localStorage.setItem("gods_id", $(this).attr("index"))
                location.href = "data.html"
            })
            $("body>section>.box2 .hot1>section img").on("click", function () {
                localStorage.setItem("gods_id", $(this).attr("index"))
                location.href = "data.html"
            })
        }
    }
})

// 服饰鞋靴
$.ajax({
    url: "http://localhost:8888/goods/list",
    data: {
        category: "手机相机",
        pagesize: "1000",
    },
    success: function (res) {
        const num = res.list.length - 21
        const index = Math.floor(Math.random() * num)
        var str = ``
        for (let i = index; i < index + 20; i++) {
            str += `
            <div>
                <img src="${res.list[i].img_big_logo}" alt="" index="${res.list[i].goods_id}">
                <p>${res.list[i].title}</p>
                <span>
                    <h3>现价￥${res.list[i].current_price}</h3>
                    <del>原价￥${res.list[i].price}</del>
                    <button index="${res.list[i].goods_id}">立即购买</button>
                </span>
            </div>
            `
        }
        $("body>section>.box2 .hot2>section")[0].innerHTML = str
        $("body>section>.box2 .hot2>section button").on("click", function () {
            localStorage.setItem("gods_id", $(this).attr("index"))
            location.href = "data.html"
        })
        $("body>section>.box2 .hot2>section img").on("click", function () {
            localStorage.setItem("gods_id", $(this).attr("index"))
            location.href = "data.html"
        })
    }
})

// 海外直购
$.ajax({
    url: "http://localhost:8888/goods/list",
    data: {
        category: "海外购",
        pagesize: "1000",
    },
    success: function (res) {
        console.log(res);
        const num = res.list.length - 21
        const index = Math.floor(Math.random() * num)
        var str = ``
        for (let i = index; i < index + 20; i++) {
            str += `
            <div>
                <img src="${res.list[i].img_big_logo}" alt="" index="${res.list[i].goods_id}">
                <p>${res.list[i].title}</p>
                <span>
                    <h3>现价￥${res.list[i].current_price}</h3>
                    <del>原价￥${res.list[i].price}</del>
                    <button index="${res.list[i].goods_id}">立即购买</button>
                </span>
            </div>
            `
        }
        $("body>section>.box2 .hot3>section")[0].innerHTML = str
        $("body>section>.box2 .hot3>section button").on("click", function () {
            localStorage.setItem("gods_id", $(this).attr("index"))
            location.href = "data.html"
        })
        $("body>section>.box2 .hot3>section img").on("click", function () {
            localStorage.setItem("gods_id", $(this).attr("index"))
            location.href = "data.html"
        })
    }
})
// 锚点定位
window.addEventListener("scroll", function () {
    const Top = Math.ceil(document.documentElement.scrollTop)

    if (Top > 600) {
        $("body>section>.box2>.box3").css({
            display: "block",
        })
    } else {
        $("body>section>.box2>.box3").css({
            display: "none",
        })
    }
})
$("body>section>.box2>.box3 a:nth-last-of-type(1)").on("click", function () {
    document.documentElement.scrollTop = 0
    return false
})
$("body>section>.box2 .box2-top li").on("click", function () {
    location.href = "https://www.baidu.com/"
})
// TAB切换
$("body>.box nav div .right li>a").on("click", function (e) {
    const Text = e.target.innerText
    if (Text === "首页") {
        location.href = "home.html"
    } else if (Text === "考拉海购出品" && localStorage.getItem("gods_id")) {
        location.href = "data.html"
    } else {
        $(this).attr("class", "active").parent().siblings().children().removeAttr("class")

    }

    return false
})
// 分类列表
$.ajax({
    url: "http://localhost:8888/goods/category",
    method: "get",
    success: function (res) {
        console.log(res);
        const arr = res.list
        for (var i = 1; i < 13; i++) {
            $(`<li>${arr[i]}</li>`)
            $(".left>ul").append($(`<li>${arr[i]}<b>&gt;</b></li>`))
        }
        $(".left>ul>li").on('click',function(){
            let str = $(this)[0].innerText.slice(0,$(this)[0].innerText.length-2)
            console.log(str);
            localStorage.setItem("category",str)
            localStorage.removeItem("value")
            location.href="list.html"
        })
    }
})
// 购物车跳转
$("nav section .cart").on("click", function () {
    location.href = "cart.html"
    return false
})
$("body>section>.box2>.box3 a:nth-of-type(1)").on("click", function () {
    location.href = "cart.html"
    return false
})
// 搜索内容
$("nav section .search .box div input").on("input", function () {
    $("nav section .search span").on("click",function(){
        let value = $("nav section .search .box div input")[0].value
        localStorage.setItem("value",value)
        console.log(value);
        setTimeout(function(){
            $("nav section .search .box div input")[0].value = ``
        },500)
        location.href="list.html"
    })
    $("body").on("keydown",function(e){
        if(e.keyCode===13){
            let value = $("nav section .search .box div input")[0].value
            localStorage.setItem("value",value)
            setTimeout(function(){
                $("nav section .search .box div input")[0].value = ``
            },500)
            localStorage.removeItem("category")
            location.href="list.html"
        }
    })
})